package sebjul.ecom.entities;

import javax.persistence.*;

import java.util.*;

@Entity
@NamedQueries({
	@NamedQuery(name="findAllArticles", query="SELECT a FROM Article a"),
	@NamedQuery(name="findAllArticlesByGenre", query="SELECT a FROM Article a"),
	@NamedQuery (name="findAllArticlesByPage", query="SELECT a FROM Article a ORDER BY a.title"),
	@NamedQuery (name="countArticles",query="SELECT COUNT(a) FROM Article a"),
//	@NamedQuery(name="test", query="SELECT a FROM Article a WHERE a.id IN (SELECT Article_FK FROM article_genre WHERE Genre_FK='45')"),
	@NamedQuery(name="test2", query="select a from Article a join a.genres g where g.id = :genreid"),
	@NamedQuery(name="countgenre", query="select COUNT(a) from Article a join a.genres g where g.id = :genreid")
})
public class Article implements Comparable {
	
	@Id @GeneratedValue
	private int id;
	
	@Column(nullable=false)
	private String title;
	
	private String author;
	
	@Column (length=400)
	private String summary;
	
	private int numberOfPages;

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name="Article_Genre",
						joinColumns = @JoinColumn(name="Article_FK"),
						inverseJoinColumns = @JoinColumn(name="Genre_FK"))
	private List<Genre> genres;
	
	private double price;
	
	private int year;
	
	private int stock;

	private String editor;

	private String Language;
	
	private long isbn;
	
	@Override
	public boolean equals (Object other) {
		if (other instanceof Article) {
			if (id == ((Article)other).id)
				return true;
		}
		return false;
	}
	
	@Override
	public int hashCode () {
		return id;
	}
	
	@Override
	public int compareTo(Object other) {
		Article otherArticle = (Article) other;
		if (otherArticle.title.equals(title)) {
			if (otherArticle.author != null && author != null) {
				if (otherArticle.author.equals(author))
					return (id-otherArticle.id);
				else
					return author.compareToIgnoreCase(otherArticle.author);
			}
			return (id-otherArticle.id);
		} else
		return title.compareToIgnoreCase(otherArticle.title);
	}
	
	public Article () {
		genres = new ArrayList();
	}
	
	public int getId() {
		return id;
	}
	
	public void setId (int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public int getNumberOfPages() {
		return numberOfPages;
	}

	public void setNumberOfPages(int numberOfPages) {
		this.numberOfPages = numberOfPages;
	}

	public List<Genre> getGenres() {
		return genres;
	}

	public void setGenres(List<Genre> genres) {
		this.genres = genres;
	}
	
	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public String getEditor() {
		return editor;
	}

	public void setEditor(String editor) {
		this.editor = editor;
	}

	public String getLanguage() {
		return Language;
	}

	public void setLanguage(String language) {
		Language = language;
	}

	public long getIsbn() {
		return isbn;
	}

	public void setIsbn(long isbn) {
		this.isbn = isbn;
	}

	public void addGenre (Genre genre) {
		genres.add(genre);
	}
	
	public void removeGenre (Genre genre) {
		genres.remove(genre);
	}
	
	public String toString () {
		return title;
	}
	
	public int getStock() {
		return stock;
	}

	public void setStock(int stock) {
		this.stock = stock;
	}
}
